package cn.tangwei.org.service.impl;

import cn.tangwei.basic.service.impl.BaseServiceImpl;
import cn.tangwei.basic.utils.PageList;
import cn.tangwei.org.domain.Department;
import cn.tangwei.org.mapper.DepartmentMapper;
import cn.tangwei.org.query.DepartmentQuery;
import cn.tangwei.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;
    // 基本的CRUD 分页加高级查询 批量删除通过继承BaseServiceImpl得到


    @Override
    public void add(Department department) {
        departmentMapper.save(department);// 返回自增id
        this.update(department);

    }

    @Override
    public void update(Department department) {
        // 封装dirPath
        String dirPath = "";
        if(department.getDepartment() == null){// 新增顶级部门
            dirPath = "/" + department.getId();
        }else {// 新增非顶级部门
            Department byId = departmentMapper.queryById(department.getDepartment().getId());
            dirPath = byId.getDirPath() + "/" + department.getId();
        }
        department.setDirPath(dirPath);
        departmentMapper.update(department);
    }

    // 自己写的自定义方法不能抽取
    @Override
    public List<Department> queryTree() {
        return departmentMapper.queryTree();
    }
}
